<?php
	include('../configs/connect.php');
	include('db.php');
	$db = new db_mysql();
	$db->connect($host,$name,$pass,$database);
	if(isset($_GET['cat'])){
		$cat = $_GET['cat'];
		$donvi = $_GET['donvi'];
		$hocvu = $_GET['hocvu'];
		if($cat=="mon"){
			$sql = "SELECT DISTINCT mh.ten_mh, mh.madm_mh from dm_monhoc AS mh
					INNER JOIN tbl_mon_ctdt AS mctdt ON mctdt.ma_monhoc = mh.madm_mh
					INNER JOIN tbl_lopmon_monctdt AS lmctdt ON lmctdt.ma_mon_ctdt = mctdt.ma_mon_ctdt
					INNER JOIN tbl_lopmon AS lm ON lm.ma_lopmon = lmctdt.ma_lopmon
					WHERE lm.ma_trungtam = '$donvi' AND lm.ma_donvihocvu = '$hocvu'";
			$marr = $db->getList($sql);
				echo "<option value=\"00\">Chọn môn thi</option>";
			foreach($marr as $mon){
				echo "<option value=\"".$mon['madm_mh']."\">".$mon['ten_mh']."</option>";
			}
		}
	}
	else if(isset($_GET['action'])){
		if($_GET['action']=="themds"){
			$mahoidong = $_POST['mahoidong'];
			$mamon = $_POST['monthi'];
			$cathi = $_POST['cathi'];
			$ngaythi = strtotime($_POST['ngaythi']);
			$sods = $_POST['dsso'];
			$svadd = preg_split("/,/",$_POST['push']);
			$svdel = preg_split("/,/",$_POST['pop']);
			$madsthi = $mahoidong.$mamon.$sods;
			$sql = "INSERT INTO tbl_dsthi(ma_dsthi, ngaythi, ma_hoidongthi, madm_ca, ma_mon_ctdt, trangthai_dsthi) 
					VALUES ('$madsthi', '$ngaythi', '$mahoidong', '$cathi', '$mamon', '$sods')";
			$db->query($sql);
			

			foreach($svadd as $mdkm){
				$db->query("INSERT INTO tbl_diemthi(ma_diemthi, ma_dkm, ma_dsthi, lanthi) VALUES ('$mdkm', '$mdkm', '$madsthi', 1)");
			}
			foreach($svdel as $mdkm){
				$db->query("DELETE FROM tbl_diemthi WHERE ma_dkm='$mdkm' AND ma_dsthi = '$madsthi'");
			}
			header('Location: index.php?muc=tochucthi&modul=lapdsthi_filter');
		}
		else if($_GET['action']=="suads"){
			$madsthi = $_POST['madsthi'];
			$cathi = $_POST['cathi'];
			$ngaythi = strtotime($_POST['ngaythi']);
			$sods = $_POST['dsso'];
			if(!empty($_POST['push']))
			$svadd = preg_split("/,/",$_POST['push']);
			if(!empty($_POST['pop']))
			$svdel = preg_split("/,/",$_POST['pop']);

			$sql = "UPDATE tbl_dsthi SET ngaythi = '$ngaythi', madm_ca = '$cathi', trangthai_dsthi = '$sods' 
					WHERE ma_dsthi = '$madsthi'";
			$db->query($sql);
			
			if(isset($svadd)){
			foreach($svadd as $mdkm){
				$db->query("INSERT INTO tbl_diemthi(ma_diemthi, ma_dkm, ma_dsthi, lanthi) VALUES ('$mdkm', '$mdkm', '$madsthi', 1)");
			}}
			if(isset($svdel)){
			foreach($svdel as $mdkm){
				$db->delete("tbl_diemthi", "ma_dkm='$mdkm' AND ma_dsthi = '$madsthi'");
			}}
			header('Location: index.php?muc=tochucthi&modul=lapdsthi_filter');
		}
		else if($_GET['action']=="dsbyhoidong"){
			$madonvi = $_GET['donvi'];
			$mahocvu = $_GET['hocvu'];
			$mahoidong = $db->getOneField("SELECT ma_hoidong FROM tbl_hoidongthi WHERE ma_donvihocvu = '$mahocvu' AND ma_trungtam = '$madonvi'");
			$sql = "SELECT dst.ma_dsthi, mh.ten_mh, dst.ngaythi, ch.khungthoigian FROM tbl_dsthi AS dst
										INNER JOIN dm_cahoc AS ch ON ch.madm_ca = dst.madm_ca
										INNER JOIN dm_monhoc AS mh ON mh.madm_mh = dst.ma_mon_ctdt WHERE dst.ma_hoidongthi = '$mahoidong'";
			if(isset($_GET['mon'])){
				$mon = $_GET['mon'];
				$sql .= " AND mh.madm_mh = '$mon'";
			}
			$listdsthi = $db->getList($sql);
			echo "	<table><tbody>
			<tr class='alt-row'>
				<th>Mã DS Thi</th>
				<th>Môn thi</th>
				<th>Ngày thi</th>
				<th>Ca thi</th>
				<th colspan='2'>Chức năng</th>
			</tr>
			";
			foreach($listdsthi as $dsthi){
				echo "<tr>
				<td>".$dsthi['ma_dsthi']."</td>
				<td>".$dsthi['ten_mh']."</td>
				<td>".date('d-m-Y', $dsthi['ngaythi'])."</td>
				<td>".$dsthi['khungthoigian']."</td>
				<td><a href='index.php?muc=tochucthi&modul=suadsthi&dsthi=".$dsthi['ma_dsthi']."'><img src='../php/images/edit.png'></a></td>
				<td><a href='#' class='delds' data-url='".$dsthi['ma_dsthi']."'><img src='../php/images/del.png'></a></td>
				</tr>";
			}
			echo "</tbody></table>";
		}
		else if($_GET['action']=="xoads"){
			$mads = $_GET['mads'];
			$db->delete('tbl_dsthi', "ma_dsthi = '$mads'");
			if($db->affected()){
				echo 1;
			} else echo 0;
		}
	}

?>